//
//  Buzz_API_DemoAppDelegate.m
//  Buzz API Demo
//
//  Created by Peter Hopkins on 4/22/10.
//  Copyright Google Inc 2010. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "Buzz_API_DemoAppDelegate.h"

#import "BuzzDao.h"
#import "PostListViewController.h"
#import "OAuthViewControllerTouch+Buzz.h"

#import "GDataOAuthAuthentication.h"
#import "GDataOAuthViewControllerTouch.h"


@interface Buzz_API_DemoAppDelegate (PrivateMethods)
- (GDataOAuthAuthentication *)makeAuthentication;
@end

@implementation Buzz_API_DemoAppDelegate

@synthesize window = window_;
@synthesize mainController = mainController_;
@synthesize postListViewController = postListViewController_;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  buzzDao_ = [[BuzzDao alloc] initWithDelegate:self];

  if (![buzzDao_ loadStore]) {
    abort();
  }

  [postListViewController_ setDao:buzzDao_];

  authentication_ = [[self makeAuthentication] retain];

  if ([authentication_ canAuthorize]) {
    [buzzDao_ setAuthorizer:authentication_];
  }

  // Add the tab bar controller's current view as a subview of the window
  [window_ addSubview:[mainController_ view]];

  if (![authentication_ canAuthorize]) {
    [self pushModalOAuthSignIn:authentication_];
  }  
}


- (void)dao:(BuzzDao *)dao storageError:(NSError *)error {
  NSLog(@"Unresolved storage error %@, %@", error, [error userInfo]);
}


- (void)dao:(BuzzDao *)dao networkError:(NSError *)error {
  NSLog(@"Unresolved network error %@, %@", error, [error userInfo]);
}


- (GDataOAuthAuthentication *)makeAuthentication {
  return [GDataOAuthViewControllerTouch
          authForGoogleFromKeychainForName:kAppServiceName];
}


- (void)signOut:(id)sender {
   NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
   for (int i = 0; i < [cookies count]; ++i) {
     [[NSHTTPCookieStorage sharedHTTPCookieStorage]
      deleteCookie:[cookies objectAtIndex:i]];
   }
   
   [GDataOAuthViewControllerTouch
        revokeTokenForGoogleAuthentication:authentication_];
   [GDataOAuthViewControllerTouch
       removeParamsFromKeychainForName:kAppServiceName];

  [postListViewController_ reset];
  [buzzDao_ reset];

  [authentication_ release];
  authentication_ = [self makeAuthentication];
  [buzzDao_ setAuthorizer:authentication_];
  [self pushModalOAuthSignIn:authentication_];
}


- (void)pushModalOAuthSignIn:(GDataOAuthAuthentication *)auth {
  oauthViewController_ = [[GDataOAuthViewControllerTouch alloc]
                          initForBuzzWithScope:kBuzzScope
                          language:nil
                          appServiceName:kAppServiceName 
                          authentication:auth
                          delegate:self
                          finishedSelector:
                          @selector(viewController:finishedWithAuth:error:)];

  [[oauthViewController_ navigationItem] setTitle:@"Google Buzz"];

  oauthNavViewController_ = [[UINavigationController alloc]
                             initWithRootViewController:oauthViewController_];

  [mainController_ presentModalViewController:oauthNavViewController_
                                     animated:YES];
}


- (void)viewController:(GDataOAuthViewControllerTouch *)viewController
      finishedWithAuth:(GDataOAuthAuthentication *)auth
                 error:(NSError *)error {

  [oauthViewController_ autorelease];
  oauthViewController_ = nil;
  [oauthNavViewController_ autorelease];
  oauthNavViewController_ = nil;

  authentication_ = [auth retain];

  if (error != nil) {
    [mainController_ dismissModalViewControllerAnimated:NO];
    [self pushModalOAuthSignIn:auth];
  } else {
    [buzzDao_ setAuthorizer:auth];
    [mainController_ dismissModalViewControllerAnimated:YES];
  }
}


- (void)dealloc {
  [buzzDao_ release];

  [window_ release];
  [mainController_ release];
  [postListViewController_ release];

  [oauthNavViewController_ release];
  [oauthViewController_ release];
  [authentication_ retain];

  [super dealloc];
}

@end

